//
//  ZGWShiLiBaoGaoSimularityPeopleViewController.m
//  1gene
//
//  Created by 张国文 on 16/6/20.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "ZGWShiLiBaoGaoSimularityPeopleViewController.h"
#import "ZGWShiLiBaoGaoSimularityPeopleModel.h"
#import "ZGWShiLiBaoBaoSimularityPeopleTableViewCell.h"
#import "AddPeopleAndGroupViewController.h"
#import "MyAttentionViewController.h"

#define kCurrentUserToken [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]

#define kCurrentUserID [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"]

@interface ZGWShiLiBaoGaoSimularityPeopleViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *simularityPeopleTableView;
@property (nonatomic, strong) NSMutableDictionary *isClickHeader; // 记录是否点击表头
@property (nonatomic, strong) NSMutableArray *keyArray; // 记录每个表头的名字
@property (nonatomic, strong) NSMutableDictionary *dataArrayOfKeyDic; // 记录每个表头对应的数组数据源
@property (nonatomic, strong) NSMutableDictionary *countOfKeyDic; // 记录每个分区需要展示的行数

@property (nonatomic, strong) NSString *minePosition; // 我的位置
@property (nonatomic, strong) NSString *totolPeolple; // 总人数

@property (nonatomic, strong) NSTimer *myTimer; // 检查请求是否超时
@property (nonatomic, assign) NSInteger numberOfTime; // 存储请求时间

@end

@implementation ZGWShiLiBaoGaoSimularityPeopleViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:20]};
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
    [self.myTimer invalidate];
    self.numberOfTime = 0;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:22]};
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // navi的修改
    [self changeNavigation];
    // 检查网络并且获取数据源
    [self searchNetAndAddData];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    if ([self.isGaiBianQianOrGaiBianHou isEqualToString:@"0"]) {
        self.navigationItem.title = @"壹基因健康指数用户排名";
    } else if ([self.isGaiBianQianOrGaiBianHou isEqualToString:@"1"]) {
        self.navigationItem.title = @"风险矫正后健康指数排名";
    }
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"baisesousuokuang-liaotianshi"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    
    UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchMorePeople:)];
    [rightImageView addGestureRecognizer:rightTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 搜索
- (void)searchMorePeople:(UITapGestureRecognizer *)tapGes
{
    AddPeopleAndGroupViewController *addPeopleAndGroupVC = [[AddPeopleAndGroupViewController alloc] init];
    [self.navigationController pushViewController:addPeopleAndGroupVC animated:YES];
}

#pragma mark - 检查网络并建立数据源
- (void)searchNetAndAddData
{
    if ([[[NetStatusHandle shareHandle] getNetStatus] isEqualToString:@"YES"]) {
        // 说明有网络
        // 建立数据源
        [self addData];
       
            // 说明不是示例报告
            // 建立计时器，计算是否超时
            self.myTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(goTimeOnReportCommentPeople:) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
            self.numberOfTime = 0;
            // 开始动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
        
    } else {
        NSLog(@"没有网络");
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kNoNetNoRefresh ToView:self];
    }
}

#pragma mark - 示例报告推荐人timer
- (void)goTimeOnReportCommentPeople:(NSTimer *)timer
{
    self.numberOfTime++;
    NSLog(@"示例报告推荐人时间= %ld", self.numberOfTime);
    if (self.numberOfTime > kZGWQingQiuChaoShi) {
        NSLog(@"示例报告推荐人请求超时");
        self.numberOfTime = 0;
        [self.myTimer invalidate];
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kQingQiuChaoShiNoReFresh ToView:self];
    }
}

#pragma mark - 添加数据源
- (void)addData
{
    // 推荐人url
    NSString *commentUrl = [NSString stringWithFormat:@"%@?token=%@", KZGWGetCommentUserOfExampleReport, kCurrentUserToken];
    NSDictionary *paramaterDic = [NSDictionary dictionary];
    if ([self.isGaiBianQianOrGaiBianHou isEqualToString:@"0"]) {
        // 改变前
        paramaterDic = @{@"keyword":@"health_score"};
    } else if ([self.isGaiBianQianOrGaiBianHou isEqualToString:@"1"]) {
        // 改变后
        paramaterDic = @{@"keyword":@"health_score_modifed"};
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:commentUrl parameters:paramaterDic success:^(NSURLSessionDataTask *task, id responseObject) {
        // 将计时器置为0
        self.numberOfTime = 0;
        [self.myTimer invalidate];
        // 结束动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        if (!responseObject) {
            return;
        }
        // 五星级
        NSArray *highArray = [responseObject valueForKey:@"high"];
        NSMutableArray *wuXingJiArray = [NSMutableArray array];
        for (NSDictionary *tempDic in highArray) {
            ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            NSString *currentUserID = [NSString stringWithFormat:@"%@", kCurrentUserID];
            if ([currentUserID isEqualToString:[tempDic valueForKey:@"user_id"]]) {
                [model setValue:@"YES" forKey:@"isMine"];
            } else {
                [model setValue:@"NO" forKey:@"isMine"];
            }
            [wuXingJiArray addObject:model];
        }
        // 四星级
        NSArray *middleArray = [responseObject valueForKey:@"middle"];
        NSMutableArray *siXingJiArray = [NSMutableArray array];
        for (NSDictionary *tempDic in middleArray) {
            ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            NSString *currentUserID = [NSString stringWithFormat:@"%@", kCurrentUserID];
            if ([currentUserID isEqualToString:[tempDic valueForKey:@"user_id"]]) {
                [model setValue:@"YES" forKey:@"isMine"];
            } else {
                [model setValue:@"NO" forKey:@"isMine"];
            }
            [siXingJiArray addObject:model];
        }
        // 三星级
        NSArray *lowArray = [responseObject valueForKey:@"low"];
        NSMutableArray *sanXingJiArray = [NSMutableArray array];
        for (NSDictionary *tempDic in lowArray) {
            ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            NSString *currentUserID = [NSString stringWithFormat:@"%@", kCurrentUserID];
            if ([currentUserID isEqualToString:[tempDic valueForKey:@"user_id"]]) {
                [model setValue:@"YES" forKey:@"isMine"];
            } else {
                [model setValue:@"NO" forKey:@"isMine"];
            }
            [sanXingJiArray addObject:model];
        }
        
        self.dataArrayOfKeyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:wuXingJiArray, @"五星用户", siXingJiArray, @"四星用户", sanXingJiArray, @"三星用户", nil];
        self.keyArray = [NSMutableArray arrayWithObjects:@"五星用户", @"四星用户", @"三星用户", nil];
        // 每个分区需要展示的行数
        self.countOfKeyDic = [NSMutableDictionary dictionary];
        if (wuXingJiArray.count < 2) {
            [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", wuXingJiArray.count] forKey:@"五星用户"];
        } else {
            [self.countOfKeyDic setValue:@"2" forKey:@"五星用户"];
        }
        if (siXingJiArray.count < 2) {
            [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", siXingJiArray.count] forKey:@"四星用户"];
        } else {
            [self.countOfKeyDic setValue:@"2" forKey:@"四星用户"];
        }
        if (sanXingJiArray.count < 2) {
            [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", sanXingJiArray.count] forKey:@"三星用户"];
        } else {
            [self.countOfKeyDic setValue:@"2" forKey:@"三星用户"];
        }
        // 初始化表头的点击字典
        self.isClickHeader = [NSMutableDictionary dictionary];
        for (int i = 0; i < self.keyArray.count; i++) {
            NSString *key = self.keyArray[i];
            if ([key isEqualToString:@"五星用户"]) {
                [self.isClickHeader setValue:@"YES" forKey:key];
            } else {
                [self.isClickHeader setValue:@"NO" forKey:key];
            }
        }
        // 我目前的位置
        self.minePosition = [NSString stringWithFormat:@"%@", [responseObject valueForKey:@"rank"]];
        // 该报告总人数
        self.totolPeolple = [NSString stringWithFormat:@"%@", [responseObject valueForKey:@"sample_count"]];
        // 布局
        [self addMySubview];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kHuoQuDataShiBaiNoRefresh ToView:self];
        // 暂停计时器
        [self.myTimer invalidate];
        self.numberOfTime = 0;
    }];
    
 
    
}

#pragma mark - 建立本地模拟数据
- (void)addMoNiData
{
    // 构建模拟数据
    NSDictionary *User1 = [NSDictionary dictionaryWithObjectsAndKeys:@"周杰伦", @"name", @"0", @"gender", @"#健康达人#美食达人#美食达人#洗澡达人", @"tags", @"8.0", @"count", nil];
    NSDictionary *User2 = [NSDictionary dictionaryWithObjectsAndKeys:@"王力宏", @"name", @"1", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"7.5", @"count", nil];
    NSDictionary *User3 = [NSDictionary dictionaryWithObjectsAndKeys:@"taylorswift", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"7.0", @"count", nil];
    NSDictionary *User4 = [NSDictionary dictionaryWithObjectsAndKeys:@"凯莉布鲁克", @"name", @"0", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"6.5", @"count", nil];
    NSDictionary *User5 = [NSDictionary dictionaryWithObjectsAndKeys:@"陶喆", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"6.0", @"count", nil];
    NSDictionary *User6 = [NSDictionary dictionaryWithObjectsAndKeys:@"林俊杰", @"name", @"0", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"8.8", @"count", nil];
    NSDictionary *User7 = [NSDictionary dictionaryWithObjectsAndKeys:@"张国文", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"5.5", @"count", nil];
    NSDictionary *User8 = [NSDictionary dictionaryWithObjectsAndKeys:@"梅江林", @"name", @"0", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"5.0", @"count", nil];
    NSDictionary *User9 = [NSDictionary dictionaryWithObjectsAndKeys:@"沈佳", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"4.5", @"count", nil];
    NSDictionary *User10 = [NSDictionary dictionaryWithObjectsAndKeys:@"欧兆", @"name", @"0", @"gender", @"#健康达人#美食达人", @"tags", @"4.0", @"count", nil];
    NSDictionary *User11 = [NSDictionary dictionaryWithObjectsAndKeys:@"张慧杰", @"name", @"1", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"3.5", @"count", nil];
    NSDictionary *User12 = [NSDictionary dictionaryWithObjectsAndKeys:@"方立剑", @"name", @"0", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"3.0", @"count", nil];
    NSDictionary *User13 = [NSDictionary dictionaryWithObjectsAndKeys:@"何鸣奇", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"2.5", @"count", nil];
    NSDictionary *User14 = [NSDictionary dictionaryWithObjectsAndKeys:@"刘东", @"name", @"0", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"2.0", @"count", nil];
    NSDictionary *User15 = [NSDictionary dictionaryWithObjectsAndKeys:@"cody", @"name", @"1", @"gender", @"#健康达人#美食达人", @"tags", @"1.5", @"count", nil];
    NSDictionary *User16 = [NSDictionary dictionaryWithObjectsAndKeys:@"壹基因", @"name", @"1", @"gender", @"#健康达人#美食达人#洗澡达人", @"tags", @"1.0", @"count", nil];
    NSMutableArray *wuXingJiArray = [NSMutableArray arrayWithObjects:User1, User2, User3, User4, User5, User6, nil];
    NSMutableArray *siXingJiArray = [NSMutableArray arrayWithObjects:User7, User8, User9, User10, User11, nil];
    NSMutableArray *sanXingJiArray = [NSMutableArray arrayWithObjects:User12, User13, User14, User15, User16, nil];
    // 表头数组
    self.keyArray = [NSMutableArray arrayWithObjects:@"五星用户", @"四星用户", @"三星用户", nil];
    // 表头对应的数组
    NSMutableArray *wuXingJiModelArray = [NSMutableArray array];
    for (NSDictionary *dic in wuXingJiArray) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [wuXingJiModelArray addObject:model];
    }
    NSMutableArray *siXingJiModelArray = [NSMutableArray array];
    for (NSDictionary *dic in siXingJiArray) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [siXingJiModelArray addObject:model];
    }
    NSMutableArray *sanXingJiModelArray = [NSMutableArray array];
    for (NSDictionary *dic in sanXingJiArray) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = [[ZGWShiLiBaoGaoSimularityPeopleModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [sanXingJiModelArray addObject:model];
    }
    self.dataArrayOfKeyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:wuXingJiModelArray, @"五星用户", siXingJiModelArray, @"四星用户", sanXingJiModelArray, @"三星用户", nil];
    // 每个分区需要展示的行数
    self.countOfKeyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"2", @"五星用户", @"2", @"四星用户", @"2", @"三星用户", nil];
    // 初始化表头的点击字典
    self.isClickHeader = [NSMutableDictionary dictionary];
    for (int i = 0; i < self.keyArray.count; i++) {
        NSString *key = self.keyArray[i];
        if (i == 0) {
            [self.isClickHeader setValue:@"YES" forKey:key];
        } else {
            [self.isClickHeader setValue:@"NO" forKey:key];
        }
    }
}

#pragma mark - 布局
- (void)addMySubview
{
    self.simularityPeopleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
    self.simularityPeopleTableView.backgroundColor = [UIColor whiteColor];
    self.simularityPeopleTableView.dataSource = self;
    self.simularityPeopleTableView.delegate = self;
    self.simularityPeopleTableView.bounces = NO;
    self.simularityPeopleTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_simularityPeopleTableView];
    
    // 表头
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 * kAdjustParamater)];
    headView.userInteractionEnabled = YES;
    headView.backgroundColor = [UIColor whiteColor];
    // 当前检测人数和用户当前的位置
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 310 * kAdjustParamater, 20 * kAdjustParamater)];
    titleLabel.text = [NSString stringWithFormat:@"共有%@人检测本产品，您在%@位", self.totolPeolple, self.minePosition];
    titleLabel.textColor = kCyanColor;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k18Big];
    [headView addSubview:titleLabel];
    // 我按钮
    UIButton *mineButton = [UIButton buttonWithType:UIButtonTypeSystem];
    mineButton.frame = CGRectMake(330 * kAdjustParamater, 5 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater);
    mineButton.backgroundColor = kCyanColor;
    mineButton.layer.cornerRadius = mineButton.width / 2;
    mineButton.layer.masksToBounds = YES;
    [mineButton setTitle:@"我" forState:UIControlStateNormal];
    [mineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    mineButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k20Big];
    [mineButton addTarget:self action:@selector(clickMine:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:mineButton];
    self.simularityPeopleTableView.tableHeaderView = headView;
    
    // 表尾
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 * kAdjustParamater)];
    footerView.backgroundColor = [UIColor whiteColor];
    // 提醒三星以下用户不显示
    UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10 * kAdjustParamater, kScreenWidth, 20 * kAdjustParamater)];
    alertLabel.text = @"三星以下用户不显示哦，加油冲星吧~";
    alertLabel.textColor = kCyanColor;
    alertLabel.textAlignment = NSTextAlignmentCenter;
    alertLabel.font = [UIFont systemFontOfSize:k13Big];
    [footerView addSubview:alertLabel];
    self.simularityPeopleTableView.tableFooterView = footerView;
}

#pragma mark - 点击我
- (void)clickMine:(UIButton *)button
{
    NSLog(@"我");
    NSInteger section = -1;
    NSInteger row = -1;
    // 判断我是否在星级行列
    NSArray *wuXingJiArray = [self.dataArrayOfKeyDic valueForKey:@"五星用户"];
    NSArray *siXingJiArray = [self.dataArrayOfKeyDic valueForKey:@"四星用户"];
    NSArray *sanXingJiArray = [self.dataArrayOfKeyDic valueForKey:@"三星用户"];
    for (int i = 0; i < wuXingJiArray.count; i++) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = wuXingJiArray[i];
        if ([model.isMine isEqualToString:@"YES"]) {
            section = 0;
            row = i;
        } else if ([model.isMine isEqualToString:@"NO"]) {
            
        }
    }
    
    for (int i = 0; i < siXingJiArray.count; i++) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = siXingJiArray[i];
        if ([model.isMine isEqualToString:@"YES"]) {
            section = 1;
            row = i;
        } else if ([model.isMine isEqualToString:@"NO"]) {
            
        }
    }
    
    for (int i = 0; i < sanXingJiArray.count; i++) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = sanXingJiArray[i];
        if ([model.isMine isEqualToString:@"YES"]) {
            section = 2;
            row = i;
        } else if ([model.isMine isEqualToString:@"NO"]) {
            
        }
    }
    
    if (section < 0 || row < 0) {
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addNoDismissAlertWithTitle:@"提示" AndContent:@"您目前不在星级用户行列，请加油冲星吧" toView:self];
    } else {
        // 先解锁相应的seciton
        [self.isClickHeader setValue:@"YES" forKey:self.keyArray[section]];
        // 让相应的section的个数大于等于当前row
        NSInteger rowCount = [[self.countOfKeyDic valueForKey:self.keyArray[section]] integerValue];
        if (rowCount <= row) {
            [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", row + 1] forKey:self.keyArray[section]];
        }
        [self.simularityPeopleTableView reloadData];
        // 跳转到相应行
        [self.simularityPeopleTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:YES scrollPosition:UITableViewScrollPositionTop];
    }
    
}



#pragma mark - tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = self.keyArray[section];
    NSString *isClick = [self.isClickHeader valueForKey:key];
    NSInteger countOfSection = [[self.countOfKeyDic valueForKey:key] integerValue];
    if ([isClick isEqualToString:@"YES"]) {
        // 说明点击了
        return countOfSection;
    } else if ([isClick isEqualToString:@"NO"]) {
        return 0;
    }
    return 0;
}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simularityPeopleCell";
    ZGWShiLiBaoBaoSimularityPeopleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ZGWShiLiBaoBaoSimularityPeopleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.keyArray.count > indexPath.section) {
        NSString *key = self.keyArray[indexPath.section];
        NSArray *value = [self.dataArrayOfKeyDic valueForKey:key];
        if (value.count > indexPath.row) {
            cell.zgwSimularityPeopleModel = value[indexPath.row];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * kAdjustParamater;
}
// 表头

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *grayBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 * kAdjustParamater)];
    grayBackGroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    grayBackGroundView.userInteractionEnabled = YES;
    grayBackGroundView.tag = 200 + section;
    // 手势
    UITapGestureRecognizer *grayBackGroundTapGes = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(grayBackGroundGes:)];
    [grayBackGroundView addGestureRecognizer:grayBackGroundTapGes];
    
    // 该分段下的用户数量
    NSArray *dataArray = [self.dataArrayOfKeyDic valueForKey:self.keyArray[section]];
    
    // 用户等级
    UILabel *userLevelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 200 * kAdjustParamater, 20 * kAdjustParamater)];
    userLevelLabel.text = [NSString stringWithFormat:@"%@(%ld人)", self.keyArray[section], dataArray.count];
    userLevelLabel.textColor = [UIColor blackColor];
    userLevelLabel.textAlignment = NSTextAlignmentLeft;
    userLevelLabel.font = [UIFont systemFontOfSize:k16Big];
    [grayBackGroundView addSubview:userLevelLabel];
    
    // 箭头
    UIImageView *jianTouImageV = [[UIImageView alloc] initWithFrame:CGRectMake(340 * kAdjustParamater, 7 * kAdjustParamater, 25 * kAdjustParamater, 25 * kAdjustParamater)];
    NSString *key = self.keyArray[section];
    NSString *isClick = [self.isClickHeader valueForKey:key];
    
    if ([isClick isEqualToString:@"YES"]) {
        jianTouImageV.image = [UIImage imageNamed:@"cyanjiantou-createGroup"];
    } else if ([isClick isEqualToString:@"NO"]) {
        jianTouImageV.image = [UIImage imageNamed:@"huisejiantou-createGroup"];
    }
    [grayBackGroundView addSubview:jianTouImageV];
    return grayBackGroundView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * kAdjustParamater;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *dataArray = [self.dataArrayOfKeyDic valueForKey:self.keyArray[indexPath.section]];
    
    MyAttentionViewController *myAttentionVC = [[MyAttentionViewController alloc] init];
    myAttentionVC.barState = 1;
    if (dataArray.count > indexPath.row) {
        ZGWShiLiBaoGaoSimularityPeopleModel *model = dataArray[indexPath.row];
        myAttentionVC.userId = model.user_id;
        NSLog(@"用户昵称：%@，用户id：%@", model.nickName, model.user_id);
    }
    [self.navigationController pushViewController:myAttentionVC animated:YES];
}

#pragma mark - 点击表头的回调
- (void)grayBackGroundGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"点击的第%ld个分区", tapGes.view.tag - 200);
    
    NSString *key = self.keyArray[tapGes.view.tag - 200];
    NSString *isClick = [self.isClickHeader valueForKey:key];
    if ([isClick isEqualToString:@"YES"]) {
        [self.isClickHeader setValue:@"NO" forKey:key];
    } else if ([isClick isEqualToString:@"NO"]) {
         [self.isClickHeader setValue:@"YES" forKey:key];
    }
    [self.simularityPeopleTableView reloadData];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSString *key = self.keyArray[section];
    NSString *isClick = [self.isClickHeader valueForKey:key];
    if ([isClick isEqualToString:@"YES"]) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 33 * kAdjustParamater)];
        footerView.backgroundColor = [UIColor whiteColor];
        footerView.tag = 300 + section;
        footerView.userInteractionEnabled = YES;
        // 手势
        UITapGestureRecognizer *footerTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(footerGes:)];
        [footerView addGestureRecognizer:footerTapGes];
        
        // 省略符号
        UIImageView *shengLueImageV = [[UIImageView alloc] initWithFrame:CGRectMake((375 - 22) / 2 * kAdjustParamater, 14 * kAdjustParamater, 22 * kAdjustParamater, 5 * kAdjustParamater)];
        shengLueImageV.image = [UIImage imageNamed:@"biaoweishenglue-shilibaogao"];
        [footerView addSubview:shengLueImageV];
        return footerView;
    }
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 5 * kAdjustParamater)];
    return footerView;
}

#pragma mark - 表尾的点击
- (void)footerGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"点击的第%ld表尾", tapGes.view.tag - 300);
    NSString *key = self.keyArray[tapGes.view.tag - 300];
    NSArray *valueArray = [self.dataArrayOfKeyDic valueForKey:key];
    NSInteger countOfSection = [[self.countOfKeyDic valueForKey:key] integerValue];
    if (valueArray.count >= (countOfSection +2)) {
        // 说明点击的时候可以增加两行
        [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", countOfSection + 2] forKey:key];
    } else if (valueArray.count == countOfSection) {
        // 说明数据源数组已经展示完全
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertNoTitieWithContent:@"没有更多了" toView:self];
    } else {
        // 直接展示数据源数组的个数
        [self.countOfKeyDic setValue:[NSString stringWithFormat:@"%ld", valueArray.count] forKey:key];
    }
    [self.simularityPeopleTableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSString *key = self.keyArray[section];
    NSString *isClick = [self.isClickHeader valueForKey:key];
    if ([isClick isEqualToString:@"YES"]) {
        return 33 * kAdjustParamater;
    }
    return 5 * kAdjustParamater;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
